package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysProject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.vo.SysEngineeringFileVo;
import com.ruoyi.system.mapper.SysProjectMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysEngineeringFileMapper;
import com.ruoyi.system.domain.SysEngineeringFile;
import com.ruoyi.system.service.ISysEngineeringFileService;
import org.springframework.util.StringUtils;

/**
 * 工程文件Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-29
 */
@Service
public class SysEngineeringFileServiceImpl implements ISysEngineeringFileService {
    @Autowired
    private SysEngineeringFileMapper sysEngineeringFileMapper;

    @Autowired
    private SysProjectMapper sysProjectMapper;

    /**
     * 查询工程文件
     *
     * @param fileId 工程文件主键
     * @return 工程文件
     */
    @Override
    public SysEngineeringFile selectSysEngineeringFileByFileId(Long fileId) {
        return sysEngineeringFileMapper.selectSysEngineeringFileByFileId(fileId);
    }

    /**
     * 查询工程文件列表
     *
     * @param sysEngineeringFile 工程文件
     * @return 工程文件
     */
    @Override
    public List<SysEngineeringFileVo> selectSysEngineeringFileList(SysEngineeringFile sysEngineeringFile) {

        List<SysEngineeringFileVo> result = new ArrayList<>();

        List<SysEngineeringFile> list = sysEngineeringFileMapper.selectSysEngineeringFileListV2(sysEngineeringFile);

        for (SysEngineeringFile engineeringFile : list) {

            SysEngineeringFileVo vo = new SysEngineeringFileVo();
            BeanUtils.copyProperties(engineeringFile, vo);

            SysProject sysProject = sysProjectMapper.selectSysProjectByProjectId(engineeringFile.getProjectId());

            if (sysProject != null) {

                vo.setProjectName(sysProject.getProjectName());
                vo.setProjectType(sysProject.getProjectType());
                vo.setProjectBelong(sysProject.getProjectBelong());
            }

            // 获取文件类型
            String fileUrl = engineeringFile.getFileUrl();
            if (StringUtils.hasText(fileUrl)) {
                String fileType = getFileType(fileUrl);
                vo.setFileType(fileType);
            }
            result.add(vo);

        }


        return result;
    }

    /**
     * 新增工程文件
     *
     * @param sysEngineeringFile 工程文件
     * @return 结果
     */
    @Override
    public int insertSysEngineeringFile(SysEngineeringFile sysEngineeringFile) {
        sysEngineeringFile.setCreateTime(DateUtils.getNowDate());
        return sysEngineeringFileMapper.insertSysEngineeringFile(sysEngineeringFile);
    }

    /**
     * 修改工程文件
     *
     * @param sysEngineeringFile 工程文件
     * @return 结果
     */
    @Override
    public int updateSysEngineeringFile(SysEngineeringFile sysEngineeringFile) {
        sysEngineeringFile.setUpdateTime(DateUtils.getNowDate());
        return sysEngineeringFileMapper.updateSysEngineeringFile(sysEngineeringFile);
    }

    /**
     * 批量删除工程文件
     *
     * @param fileIds 需要删除的工程文件主键
     * @return 结果
     */
    @Override
    public int deleteSysEngineeringFileByFileIds(Long[] fileIds) {
        return sysEngineeringFileMapper.deleteSysEngineeringFileByFileIds(fileIds);
    }

    /**
     * 删除工程文件信息
     *
     * @param fileId 工程文件主键
     * @return 结果
     */
    @Override
    public int deleteSysEngineeringFileByFileId(Long fileId) {
        return sysEngineeringFileMapper.deleteSysEngineeringFileByFileId(fileId);
    }

    /**
     * 从文件URL中提取文件类型（扩展名）
     * @param fileUrl 文件URL
     * @return 文件扩展名（如：xlsx），如果没有扩展名则返回空字符串
     */
    private String getFileType(String fileUrl) {
        if (!StringUtils.hasText(fileUrl)) {
            return "";
        }

        // 获取最后一个点的位置
        int lastDotIndex = fileUrl.lastIndexOf('.');
        // 获取最后一个斜杠的位置，确保我们获取的是文件名中的扩展名
        int lastSlashIndex = Math.max(fileUrl.lastIndexOf('/'), fileUrl.lastIndexOf('\\'));

        // 确保点号在最后一个斜杠之后（即确实是文件扩展名）
        if (lastDotIndex > lastSlashIndex && lastDotIndex < fileUrl.length() - 1) {
            return fileUrl.substring(lastDotIndex + 1).toLowerCase();
        }

        return "";
    }
}
